/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package queenb.web.controller;

import java.util.Date;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.faces.event.ActionEvent;
import javax.faces.event.ValueChangeEvent;
import javax.faces.model.SelectItem;
import queenb.net.entitybean.A000;
import queenb.net.entitybean.V220;
import queenb.web.crm.store.CustomerStore;
import queenb.web.store.A000Store;
import queenb.web.supplier.store.V220Store;
import queenb.web.util.ComboboxUtil;
import queenb.web.util.ContextUtil;
import queenb.web.util.SessionProvider;
import queenb.web.util.Util;

/**
 *
 * @author Nguyen Hung
 */
public class serviceController1 extends QueenbController {

    private int pv220;
    private int pk100;
    private int fv200;// id of service in setting
    private int fa000;// id of city
    private int fn750;// id of country
    private String vv223;// sevice name
    private static int vn224;// feetbet
    private String vv225;// Description
    private Date vd221;
    private Date vd222;
    private String vn228;// price of service
    private List<SelectItem> comboA000 = new ArrayList<SelectItem>();
    private List<SelectItem> comboA000i = new ArrayList<SelectItem>();
    private List<SelectItem> comboV200 = new ArrayList<SelectItem>();
    private List<SelectItem> comboN750 = new ArrayList<SelectItem>();
    private ComboboxUtil comboboxUtil;
    // private V220Store v220Store;
    private V220Store v220Store;
    private String messagefrom;
    private int star;
    private String pagereload;
    private int tip;

    public serviceController1() {
        Object display = getRequest().getParameter("page");
        try {
            this.pagereload = display.toString();
            if (this.pagereload == null) {
                this.pagereload = ContextUtil.getRequestParameter("page");
            }
            if (pagereload.equals("supplier.jsp")) {
                setTip(1);
            } else if (pagereload.equals("partner.jsp")) {
                setTip(0);
            }
            //this.pagereload = pages.toString();
            System.out.println("**************************" + pagereload + tip);
        } catch (Exception ex) {
        }
        v220Store = new V220Store();
        System.out.println("-----------khởi động ------" + this.fn750 + SessionProvider.getSessionMapValue("fn750"));
        Object objectPK100 = getRequest().getParameter("id");
        if (objectPK100 != null) {
            this.pk100 = Util.parseInt(objectPK100);

        }
        Object objectSN354 = getRequest().getParameter("sn354");
        Object objFN750 = getRequest().getParameter("fn750");
        if (objFN750 != null) {
            this.fn750 = Integer.parseInt(objFN750.toString());
        }
        Object objectPK100i = getRequest().getParameter("pk100");
        if (objectPK100i != null) {
            this.vn224 = Util.parseInt(objectSN354);
            this.pk100 = Util.parseInt(objectPK100i);


        }
        Object objectaction = getRequest().getParameter("action");
        try {
            if (objectaction.equals("add")) {
                SessionProvider.setSessionMapValue("fn750", 0);
                setVn224(0);
            }
        } catch (Exception e) {
        }
        try {

            Object objectPV220 = getRequest().getParameter("pv220");
            Object objectVN224 = getRequest().getParameter("vn224");
            if (objectaction.equals("favourite") && objectPV220 != null) {

                this.star = Util.parseInt(objectVN224);
                this.pv220 = Util.parseInt(objectPV220);
                System.out.println("------PKnetttttttttttttt" + Util.parseInt(objectPV220) + vn224);
                updateStar(star, pv220);
            }
        } catch (Exception e) {
        }

        try {

            Object objectPV220 = getRequest().getParameter("pv220");

            if (objectaction.equals("edit") && objectPV220 != null) {


                this.pv220 = Util.parseInt(objectPV220);

                getListT950ByID(pv220);
            }
        } catch (Exception e) {
        }



        //comboA000 = comboboxUtil.getListCombA000(fn750);
    }

    public List<V220> getListV220All() {
        try {
            System.out.println("----------Dich vuj-------" + pk100);
            List<V220> listV220 = v220Store.getListV220All(getLogin().getModule(), getLogin().getCompanyNo(), 0, 0, 0, 0, pk100, getLogin().getUserName());
            System.out.println("-----------------" + listV220.size());
            return listV220;
        } catch (Exception e) {
            return null;
        }
    }

    public void getListT950ByID(int idpv220) {
        try {
            List<V220> listV220s = getListV220All();
            if (listV220s != null) {
                for (int i = 0; i < listV220s.size(); i++) {
                    if (listV220s.get(i).getPv220() == idpv220) {
                        this.vv223 = listV220s.get(i).getVv223();
                        this.vv225 = listV220s.get(i).getVv225();
                        this.vn228 = String.valueOf(listV220s.get(i).getVn228());

                        System.out.println("-------------Description" + vn228);
                        this.vn224 = listV220s.get(i).getVn224();
                        this.fv200 = listV220s.get(i).getFv200();
                        this.fn750 = listV220s.get(i).getFn750();// quoc gia
                        SessionProvider.setSessionMapValue("fn750", fn750);
                        System.out.println("fn7500000///" + fn750);
                        this.fa000 = listV220s.get(i).getFa000();
                        System.out.println("-------------LONG LENHT" + fa000 + "FV200" + fv200);

                        this.vd221 = listV220s.get(i).getVd221();
                        this.vd222 = listV220s.get(i).getVd222();
                        this.pk100 = listV220s.get(i).getFk100();

                        break;
                    }
                }
            }
        } catch (Exception ex) {
            System.out.println("Error. ERROR CODE: " + ex.getMessage());
        }
    }

    public List<SelectItem> CombA000(int id) {
        A000Store s200Store = new A000Store();
        List<SelectItem> listItem = new ArrayList<SelectItem>();
        try {
            List<A000> listS200 = s200Store.findAllA000ForCombo(getLogin().getModule(), getLogin().getCompanyNo(), id,0, getLogin().getUserName());
            Iterator ic = listS200.iterator();
            while (ic.hasNext()) {
                A000 a000 = (A000) ic.next();
                SelectItem item = new SelectItem(a000.getVal(), a000.getLabel1());
                listItem.add(item);
            }
            // listItem.add(0,new SelectItem("0", getResourceString("please_select")));
            return listItem;
        } catch (Exception ex) {
            return null;
        }

    }

    public void updateV220() {
        try {
            String dateStart1 = ContextUtil.getRequestParameter("to");
            if (!dateStart1.equals("")) {
                vd221 = Util.parseDate(dateStart1, "dd/MM/yyyy");
            } else {
                vd221 = null;

            }
            String dateEnd1 = ContextUtil.getRequestParameter("from");
            if (!dateEnd1.equals("")) {
                vd222 = Util.parseDate(dateEnd1, "dd/MM/yyyy");
            } else {
                vd222 = null;
            }

            if (this.fa000 == 0 || vd221 == null || vd221.equals("") && dateStart1 == null || dateStart1.equals("") && dateEnd1 == null || dateEnd1.equals("")) {
                setMessagefrom("(*) Không được bỏ trống!");
            } else {


                long lDateTime = new Date().getTime();
                int longDate = (int) lDateTime;
                System.out.println("-------------SAVE" + fa000 + "FV200" + fv200 + "-----" + this.pv220 + "," + getLogin().getModule() + "," + getLogin().getCompanyNo() + "," + this.pk100 + "," + this.fa000 + "," + fv200 + "," + Util.toSQLDate(vd221) + "," + Util.toSQLDate(vd222) + "," + this.vv223 + "," + this.getVn224() + "," + this.vv225 + "," + null + "," + null + "," + vn228 + "," + getLogin().getUserName());
                int count = v220Store.updateV220(this.pv220, getLogin().getModule(), getLogin().getCompanyNo(), this.pk100, this.fa000, fv200, Util.toSQLDate(vd221), Util.toSQLDate(vd222), this.vv223, this.getVn224(), this.vv225, null, null, Double.valueOf(vn228), getLogin().getUserName());
                System.out.println("-------------SAVE" + fa000 + "FV200" + fv200);
                if (count > 0) {
                    setVn224(0);
                    setFn750(0);
                    SessionProvider.removeSessionMapValue("fn750");

                    setMessagefrom(getResourceString("success"));
                } else {
                    setMessagefrom(getResourceString("error"));
                }
            }

            //  sendRedirectURL("supplier.jsp", pk100, 6);

        } catch (Exception e) {

            System.out.println("ssssssssssssss" + e.getMessage());
        }
//        finally(Exception ex)
//        {
//            System.out.println("ssssssssssssss"+ ex.getMessage());
//        }

    }

    public void updateStar(int numberstar, int idpk100) {
        CustomerStore store = new CustomerStore();
        try {
            System.out.println("---------update column Star" + numberstar + idpk100);
            int counlumn = store.updateColumnN100(null, "V220", "VN224", String.valueOf(numberstar), "PV220", idpk100, getLogin().getUserName());
            System.out.println("---------update column " + counlumn);
        } catch (Exception e) {
        }
    }

    public void changeCombN750forA000(ValueChangeEvent event) {
        this.fn750 = Integer.parseInt(event.getNewValue().toString());
        SessionProvider.setSessionMapValue("fn750", fn750);
        System.out.println("-----------id od N750------" + this.fn750);

        //  comboA000 = comboboxUtil.getListCombA000(fn750);
//         updateV220();
    }

    public int getFa000() {
        return fa000;
    }

    /**
     * @param fa000 the fa000 to set
     */
    public void setFa000(int fa000) {
        this.fa000 = fa000;
    }

    public int getPk100() {
        return pk100;
    }

    /**
     * @param pk100 the pk100 to set
     */
    public void setPk100(int pk100) {
        this.pk100 = pk100;
    }

    /**
     * @return the comboA000
     */
    public List<SelectItem> getComboA000() {
        try {
            if (SessionProvider.getSessionMapValue("fn750") == null) {
                comboA000 = comboboxUtil.getListCombA000(0);
            } else {
                int id = Util.parseInt(SessionProvider.getSessionMapValue("fn750").toString());
                System.out.print("==========aaaaaaaaaaaaa==" + id);
                comboA000 = comboboxUtil.getListCombA000(id);
            }
        } catch (Exception e) {
        }
//        comboboxUtil = new ComboboxUtil();
        //     System.out.println("--------------id pk" + fn750);
//        comboA000 = comboboxUtil.c(fn750);
        //     comboA000 = comboboxUtil.getListCombA000(fn750);
        return comboA000;
    }

    /**
     * @param comboA000 the comboA000 to set
     */
    public void setComboA000(List<SelectItem> comboA000) {
        this.comboA000 = comboA000;
    }

    /**
     * @return the fn750
     */
    public int getFn750() {
        return fn750;
    }

    /**
     * @param fn750 the fn750 to set
     */
    public void setFn750(int fn750) {
        this.fn750 = fn750;
    }

    /**
     * @return the comboN750
     */
    public List<SelectItem> getComboN750() {
        comboboxUtil = new ComboboxUtil();
        comboN750 = comboboxUtil.getlistCombN750();
        return comboN750;
    }

    /**
     * @param comboN750 the comboN750 to set
     */
    public void setComboN750(List<SelectItem> comboN750) {
        this.comboN750 = comboN750;
    }

    public List<SelectItem> getComboA000i() {
        return comboA000i;
    }

    /**
     * @param comboA000i the comboA000i to set
     */
    public void setComboA000i(List<SelectItem> comboA000i) {
        this.comboA000i = comboA000i;
    }

    /**
     * @return the messagefrom
     */
    public String getMessagefrom() {
        return messagefrom;
    }

    /**
     * @param messagefrom the messagefrom to set
     */
    public void setMessagefrom(String messagefrom) {
        this.messagefrom = messagefrom;
    }

    /**
     * @return the pv220
     */
    public int getPv220() {
        return pv220;
    }

    /**
     * @param pv220 the pv220 to set
     */
    public void setPv220(int pv220) {
        this.pv220 = pv220;
    }

    /**
     * @return the fv200
     */
    public int getFv200() {
        return fv200;
    }

    /**
     * @param fv200 the fv200 to set
     */
    public void setFv200(int fv200) {
        this.fv200 = fv200;
    }

    /**
     * @return the vv223
     */
    public String getVv223() {
        return vv223;
    }

    /**
     * @param vv223 the vv223 to set
     */
    public void setVv223(String vv223) {
        this.vv223 = vv223;
    }

    /**
     * @return the vv225
     */
    public String getVv225() {
        return vv225;
    }

    /**
     * @param vv225 the vv225 to set
     */
    public void setVv225(String vv225) {
        this.vv225 = vv225;
    }

    /**
     * @return the vd221
     */
    public Date getVd221() {
        return vd221;
    }

    /**
     * @param vd221 the vd221 to set
     */
    public void setVd221(Date vd221) {
        this.vd221 = vd221;
    }

    /**
     * @return the vd222
     */
    public Date getVd222() {
        return vd222;
    }

    /**
     * @param vd222 the vd222 to set
     */
    public void setVd222(Date vd222) {
        this.vd222 = vd222;
    }

    /**
     * @return the vn228
     */
    public String getVn228() {
        return vn228;
    }

    /**
     * @param vn228 the vn228 to set
     */
    public void setVn228(String vn228) {
        this.vn228 = vn228;
    }

    /**
     * @return the comboV200
     */
    public List<SelectItem> getComboV200() {
        comboboxUtil = new ComboboxUtil();
        comboV200 = comboboxUtil.getListComBV200();
        return comboV200;
    }

    /**
     * @param comboV200 the comboV200 to set
     */
    public void setComboV200(List<SelectItem> comboV200) {
        this.comboV200 = comboV200;
    }

    /**
     * @return the vn224
     */
    public int getVn224() {
        return vn224;
    }

    /**
     * @param vn224 the vn224 to set
     */
    public void setVn224(int vn224) {
        this.vn224 = vn224;
    }

    /**
     * @return the star
     */
    public int getStar() {
        return star;
    }

    /**
     * @param star the star to set
     */
    public void setStar(int star) {
        this.star = star;
    }

    /**
     * @return the pagereload
     */
    public String getPagereload() {
        return pagereload;
    }

    /**
     * @param pagereload the pagereload to set
     */
    public void setPagereload(String pagereload) {
        this.pagereload = pagereload;
    }

    /**
     * @return the tip
     */
    public int getTip() {
        return tip;
    }

    /**
     * @param tip the tip to set
     */
    public void setTip(int tip) {
        this.tip = tip;
    }
}
